<?php

namespace App\Http\Controllers;

use App\Common\Tools\Qiniu;
use Illuminate\Http\Request;

class ImageController extends Controller
{
    /**
     * 图片上传
     * @param Request $request
     * @access public index
     * @return \Illuminate\Http\JsonResponse
     * @author shaowei
     */
    public function index(Request $request)
    {
        $file = $request->file('img');
        if(!empty($file)){
            $data = Qiniu::upload($request->path,file_get_contents($file->getPathname()),$file->getMimeType(),$file->getSize(),user_id());
            return $this->successReturn($data);
        }else{
            return $this->errorReturn(['error'=>'上传失败']);
        }
    }


    /**
     * 图片上传
     * @param Request $request
     * @access public index
     * @return \Illuminate\Http\JsonResponse
     * @author shaowei
     */
    public function upload(Request $request)
    {
        $files = $request->file('imgs');
        if(!empty($files)){

            //日记
            $data = [];
            foreach ($files as $file)
            {
                $data[] = Qiniu::upload($request->path,file_get_contents($file->getPathname()),$file->getMimeType(),$file->getSize(),user_id());
            }
            return $this->successReturn($data);
        }else{
            return $this->errorReturn(['error'=>'上传失败']);
        }
    }

    /**
     * 图片上传
     * @param Request $request
     * @return array
     */
    public function admin_upload(Request $request)
    {
        $urls = [];
        $file = $request->file('images');

        $data = Qiniu::upload('diary',file_get_contents($file->getPathname()),$file->getMimeType(),$file->getSize(),auth()->id());
        $urls[] = $data['host_url'];
        return [
            "errno" => 0,
            "data"  => $urls,
        ];
    }
}
